#!/usr/bin/env python
# -*- coding:utf-8 -*-
import configparser
import json

from conn.MyRedisFactory import RedisConfig
from excel.EGC.EgcBaseTabTop import EgcBaseTabTop
from excel.EGC.EgcDictUtil import EgcDictUtil
from excel.EGC.EgcObj import EgcObj
from excel.PruTuutDb import PruTuutDb

f = r'setting.ini'
config = configparser.RawConfigParser()
config.read(f, encoding='UTF8')
rds_cfg = RedisConfig(host=config['redis']['host'],
                      port=config['redis']['port'],
                      passwd=config['redis']['password'])
rds_conn = RedisConfig.get_pool(rds_cfg)

base_table_top = EgcBaseTabTop()
pru_tuut_db_conn = PruTuutDb.get_pool()
dict_db = EgcDictUtil(pru_tuut_db_conn)
# **
# 参考计算EXCEL原文件中列表右侧的数据列
# ----------------------------------------------------------------------------------------------------------
TEST_YEAR = range(100)  # 测试年限为100的数据


# ----------------------------------------------------------------------------------------------------------
class EgcBaseTabCalStd:
    res = []

    def __init__(self):
        return

    # 粘贴生成的方法内容 -------------------------------------------------------------------------------------------- START
    # 计算 SA 0
    # return: SA 0 K60: SA 0
    # Params:
    # saving_sum_assured
    def get_SA_0(self, saving_sum_assured):
        # 参考计算公式
        # =C20
        return saving_sum_assured

    # 计算 SA 1
    # return: SA 1 L60: SA 1
    # Params:
    # saving_payment_term : C6
    # repeat_saving_plan
    # repeat_saving_growth_rate
    # saving_sum_assured
    # year
    def get_SA_1(self, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate, saving_sum_assured, year):
        # 参考计算公式
        # =IF(C7>0,IF((T58-C6+1)>0,C20*(1+C8)^1,0),0)
        if not repeat_saving_plan > 0:
            return 0
        else:
            # IF((T58-C6+1)>0,C20*(1+C8)^1,0)
            if not year - saving_payment_term + 1 > 0:
                return 0
            else:
                saving_sum_assured * (1 + repeat_saving_growth_rate) ** 1
        return 0

    # 计算 SA 2
    # return: SA 2 M60: SA 2
    # Params:
    # saving_payment_term : C6
    # repeat_saving_plan
    # repeat_saving_growth_rate
    # saving_sum_assured
    # year
    def get_SA_2(self, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate, saving_sum_assured, year):
        # 参考计算公式
        # =IF(C7>1,IF((T58-C6*2+1)>0,C20*(1+C8)^2,0),0)
        if not repeat_saving_plan > 1:
            return 0
        else:
            # IF((T58-C6*2+1)>0,C20*(1+C8)^2,0)
            if not year - saving_payment_term * 2 + 1 > 0:
                return 0
            else:
                return saving_sum_assured * (1 + repeat_saving_growth_rate) ** 2

    # 计算 SA 3
    # return: SA 3 N60: SA 3
    # Params:
    # saving_payment_term : C6
    # repeat_saving_plan
    # repeat_saving_growth_rate
    # saving_sum_assured
    # year
    def get_SA_3(self, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate, saving_sum_assured, year):
        # 参考计算公式
        # =IF(C7>2,IF((T58-C6*3+1)>0,C20*(1+C8)^3,0),0)
        if not repeat_saving_plan > 2:
            return 0
        else:
            # IF((T58-C6*3+1)>0,C20*(1+C8)^3,0)
            if not year - saving_payment_term * 3 + 1 > 0:
                return 0
            else:
                return saving_sum_assured * (1 + repeat_saving_growth_rate) ** 3

    # 计算 SA 4
    # return: SA 4 O60: SA 4
    # Params:
    # saving_payment_term : C6
    # repeat_saving_plan
    # repeat_saving_growth_rate
    # saving_sum_assured
    # year
    def get_SA_4(self, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate, saving_sum_assured, year):
        # 参考计算公式
        # =IF(C7>3,IF((T58-C6*4+1)>0,C20*(1+C8)^4,0),0)
        if not repeat_saving_plan > 3:
            return 0
        else:
            # IF((T58-C6*4+1)>0,C20*(1+C8)^4,0)
            if not year - saving_payment_term * 4 + 1 > 0:
                return 0
            else:
                return saving_sum_assured * (1 + repeat_saving_growth_rate) ^ 4
        return 0

    # 计算 SA 5
    # return: SA 5 P60: SA 5
    # Params:
    # saving_payment_term : C6
    # repeat_saving_plan
    # repeat_saving_growth_rate
    # saving_sum_assured
    # year
    def get_SA_5(self, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate, saving_sum_assured, year):
        # 参考计算公式
        # =IF(C7>4,IF((T58-C6*5+1)>0,C20*(1+C8)^5,0),0)
        if not repeat_saving_plan > 4:
            return 0
        else:
            # IF((T58-C6*5+1)>0,C20*(1+C8)^5,0)
            if not year - saving_payment_term * 5 + 1 > 0:
                return 0
            else:
                return saving_sum_assured * (1 + repeat_saving_growth_rate) ** 5
        return 0

    # 计算 Total min SA
    # return: Total min SA Q60: Total min SA
    # Params:
    # 儲蓄供款期 C6 : Saving Payment Term
    # 重複儲蓄計劃 C7 : Repeat Saving Plan
    # SA minimum C45
    # Year T57
    # year
    def get_Total_min_SA(self, year, saving_payment_term, repeat_saving_plan, sa_minimum):
        # 参考计算公式
        # =(ROUNDDOWN(MIN(T57,C6*(1+C7)-1)/C6,0)+1)*C45
        # MIN(T57,C6*(1+C7)-1)
        numbers = []
        numbers.append(year)
        numbers.append(saving_payment_term * (1 + repeat_saving_plan) - 1)
        # STEP 2 (ROUNDDOWN(MIN(T58,C6*(1+C7)-1)/C6,0))
        cal_val = min(numbers) // saving_payment_term
        return (cal_val + 1) * sa_minimum

    # 计算 Running SA
    # return: Running SA R60: Running SA
    # Params:
    # nums : C6
    # remain
    def get_Running_SA(self, nums, last_remain):
        # 参考计算公式
        # =SUM(K58:P58)*Y57
        return sum(nums) * last_remain

    # 计算 SA too low
    # return: SA too low S60: SA too low
    # Params:
    # running_sa
    # total_min_sa
    def get_SA_too_low(self, running_sa, total_min_sa):
        # 参考计算公式
        # =R58<Q58
        return running_sa < total_min_sa

    # 计算 Year
    # return: Year T60: Year
    # Params:
    # anb_year
    # cal_anb
    def get_Year(self, anb_year, cal_anb, ):
        # 参考计算公式
        # =B58-C3
        return cal_anb - anb_year

    # 计算 Rebate
    # return: Rebate U60: Rebate
    # Params:
    # 儲蓄供款期 C6 saving_payment_term
    # display_rate C33
    # 推廣優惠 F10 promotion
    # egc_annual_premium C41
    # egc_premium_2 AA58
    def get_Rebate(self, year, saving_payment_term, display_rate, egc_annual_premium, promotion, egc_premium_2):
        # 参考计算公式
        # =IF(LOWER(F10)="y",IF(C6=3,IF(C41>=500000,AA58*C33*11%,IF(C41>=250000,AA58*C33*9%,IF(C41>=150000,AA58*C33*8%,IF(C41>=0,AA58*C33*7%,0)))),IF(C41>=500000,AA58*C33*23%,IF(C41>=250000,AA58*C33*16%,IF(C41>=150000,AA58*C33*14%,IF(C41>=0,AA58*C33*12%,0))))),0)
        if year > 1:
            return 0
        if not str(promotion).lower() == "y":
            return 0
        else:
            # IF(C6=3,IF(C41>=500000,AA58*C33*11%,IF(C41>=250000,AA58*C33*9%,IF(C41>=150000,AA58*C33*8%,IF(C41>=0,AA58*C33*7%,0)))),
            # IF(C41>=500000,AA58*C33*23%,IF(C41>=250000,AA58*C33*16%,IF(C41>=150000,AA58*C33*14%,IF(C41>=0,AA58*C33*12%,0)))))
            if saving_payment_term == 3:
                # C40>=500000,AA59*C32*11%, )
                if egc_annual_premium >= 500000:
                    return egc_premium_2 * display_rate * (11 / 100)
                # IF(C40>=250000,AA59*C32*9%,)
                elif egc_annual_premium >= 250000:
                    return egc_premium_2 * display_rate * (9 / 100)
                # IF(C40>=150000,AA59*C32*8%, )
                elif egc_annual_premium >= 150000:
                    return egc_premium_2 * display_rate * (8 / 100)
                # IF(C40>=0,AA59*C32*7%,0)
                elif egc_annual_premium >= 0:
                    return egc_premium_2 * display_rate * (7 / 100)
                else:
                    return 0
            else:
                # IF(C40>=500000,AA59*C32*23%,
                if egc_annual_premium >= 500000:
                    return egc_premium_2 * display_rate * (23 / 100)
                # IF(C40>=250000,AA59*C32*16%,
                if egc_annual_premium >= 250000:
                    return egc_premium_2 * display_rate * (16 / 100)
                # IF(C40>=150000,AA59*C32*14%,
                if egc_annual_premium >= 150000:
                    return egc_premium_2 * display_rate * (14 / 100)
                # IF(C40>=0,AA59*C32*12%,0)))))
                if egc_annual_premium >= 0:
                    return egc_premium_2 * display_rate * (12 / 100)
                else:
                    return 0

    # 计算 EGC premium
    # return: EGC premium V60: EGC premium
    # Params:
    # year : A57
    # cal_anb : B57
    # 儲蓄供款期 : C6 saving_payment_term
    # lumsum : C18
    # lumpsum_discount : C19
    # display_rate :C33
    # rebate: U57
    # egc_premium_2 :AA57
    def get_EGC_premium_1(self, year, anb_year, saving_payment_term, lumsum, lumpsum_discount, display_rate, rebate,
                          egc_premium_2):
        # 参考计算公式
        # =IF(B57<102,IF(AND(C18,A57<C6),IF(A57=0,AA57*C6-C19,0),AA57)*C33-U57,0)
        if not anb_year < 102:
            return 0
        else:
            # IF(AND(C18,A57<C6),IF(A57=0,AA57*C6-C19,0),AA57)
            cal_tmp1 = 0
            if year == 0:
                cal_tmp1 = egc_premium_2 * saving_payment_term - lumpsum_discount
            if lumsum and year < saving_payment_term:
                cal_val = cal_tmp1
            else:
                cal_val = egc_premium_2
            # cal_val *C33-U57
        return cal_val * display_rate - rebate

    # 计算 Cumulative premium
    # return: Cumulative premium W60: Cumulative premium
    # Params:
    def get_Cumulative_premium(self, last_cumulative_premium, egc_premium_1):
        # 参考计算公式
        # =W57+V58
        return last_cumulative_premium + egc_premium_1

    # 计算 Annual withdrawal
    # return: Annual withdrawal  X60: Annual withdrawal
    # Params:
    def get_Annual_withdrawal(self, re_adjusted_cv, total_withdrawal, display_rate):
        # 参考计算公式
        # =IF(AJ58>0,(E58/C32)/AJ58,0)
        if not re_adjusted_cv > 0:
            return 0
        else:
            return (total_withdrawal / display_rate) / re_adjusted_cv

    # 计算 Remain
    # return: Remain  Y60: Remain
    # Params:
    def get_Remain_(self, last_remain, annual_withdrawal):
        # 参考计算公式
        # =Y57-X58
        return last_remain - annual_withdrawal

    # 计算 Balance
    # return: Balance Z60: Balance
    # Params:
    def get_Balance(self, remain, re_adjusted_cv):
        # 参考计算公式
        # =Y58*AJ58
        return remain * re_adjusted_cv

    # 计算 EGC premium
    # return: EGC premium AA60: EGC premium
    # Params:
    # 翌年歲 : B57
    # 翌年歲 : C3
    # 儲蓄供款期 :C6
    # 重複儲蓄計劃 : C7
    # 重複儲蓄增長率 : C8
    # EGC annual premium : C40
    def get_EGC_premium_2(self, cal_anb, anb_year, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate,
                          egc_annual_premium):
        # 参考计算公式
        # =IF(B57-C3<(C6*(C7+1)),C40*(1+C8)^ROUNDDOWN((B57-C3)/C6,0),0)
        if not cal_anb - anb_year < (saving_payment_term * (repeat_saving_plan + 1)):
            return 0
        else:
            # C39*(1+C8)^ROUNDDOWN((B59-C3)/C6,0),0)
            return egc_annual_premium * (1 + repeat_saving_growth_rate) ** ((cal_anb - anb_year) // saving_payment_term)

    # 计算 EGC CV
    # return: EGC CV AB60: EGC CV
    # Params:
    # 翌年歲 : C3 ANB
    # saving_payment_term saving_payment_term
    # 翌年歲 B57 anb_year
    # EGC sum assured index C38
    # EGC SA factor C39
    def get_EGC_CV(self, year, ANB, anb_year, saving_payment_term, egc_sa_factor):
        # 参考计算公式
        # =VLOOKUP(B57-C3+1,data4!A4:FL124,C38,TRUE)*C39
        # STEP 1
        tab_name = EgcObj.SAVING_PAYMENT_TERM_TAB[saving_payment_term]  # C38
        # STEP 2
        anb_year = anb_year - ANB + 1
        fetch_val = dict_db.vlookup_EGC_by_anb_and_col(args=[year, anb_year], tab=tab_name, col="VAL")
        return fetch_val * egc_sa_factor

    # 计算 Adjusted EGC CV
    # return: Adjusted EGC CV Asaving_payment_term0: Adjusted EGC CV
    # Params:
    def get_Adjusted_EGC_CV(self, egc_cv):
        # 参考计算公式
        # =AB58
        return egc_cv

    # 计算 A. EGC CV 1
    # return: A. EGC CV 1 AD60: A. EGC CV 1
    # Params:
    def get_A_EGC_CV_1(self, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate, cal_anb, year, egc_cv):
        # 参考计算公式
        # =IF(C7>0,IF((T58-C6+1)>0,VLOOKUP(B58-C6,B58:AC158,C41,TRUE)*(1+C8)^1,0),0)
        if not repeat_saving_plan > 0:
            return 0
        else:
            # IF((T58-C6+1)>0,VLOOKUP(B58-C6,B58:AC158,C41,TRUE)*(1+C8)^1,0)
            if not year - saving_payment_term + 1 > 0:
                return 0
            else:
                # VLOOKUP(B58-C6,B58:AC158,C41,TRUE)*(1+C8)^1
                _val = egc_cv[cal_anb - saving_payment_term]
                return _val * (1 + repeat_saving_growth_rate) ** 1

    # 计算 A. EGC CV 2
    # return: A. EGC CV 2 AE60: A. EGC CV 2
    # Params:
    def get_A_EGC_CV_2(self, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate, cal_anb, year, egc_cv):
        # 参考计算公式
        # =IF(C7>1,IF((T58-C6*2+1)>0,VLOOKUP(B58-C6*2,B58:AC158,C41,TRUE)*(1+C8)^2,0),0)
        if not repeat_saving_plan > 0:
            return 0
        else:
            # IF((T58-C6*2+1)>0,VLOOKUP(B58-C6*2,B58:AC158,C41,TRUE)*(1+C8)^2,0)
            if not year - saving_payment_term * 2 + 1 > 0:
                return 0
            else:
                # VLOOKUP(B58-C6*2,B58:AC158,C41,TRUE)*(1+C8)^2
                _val = egc_cv[cal_anb - saving_payment_term * 2]
                return _val * (1 + repeat_saving_growth_rate) ** 1

    # 计算 A. EGC CV 3
    # return: A. EGC CV 3 Afst_Year_Withdrawal0: A. EGC CV 3
    # Params:
    def get_A_EGC_CV_3(self, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate, cal_anb, year, egc_cv):
        # 参考计算公式
        # =IF(C7>2,IF((T58-C6*3+1)>0,VLOOKUP(B58-C6*3,B58:AC158,C41,TRUE)*(1+C8)^3,0),0)
        if not repeat_saving_plan > 0:
            return 0
        else:
            # IF((T58-C6*3+1)>0,VLOOKUP(B58-C6*3,B58:AC158,C41,TRUE)*(1+C8)^3,0)
            if not year - saving_payment_term * 3 + 1 > 0:
                return 0
            else:
                # VLOOKUP(B58-C6*3,B58:AC158,C41,TRUE)*(1+C8)^3
                _val = egc_cv[cal_anb - saving_payment_term * 3]
                return _val * (1 + repeat_saving_growth_rate) ** 3

    # 计算 A. EGC CV 4
    # return: A. EGC CV 4 AG60: A. EGC CV 4
    # Params:
    def get_A_EGC_CV_4(self, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate, cal_anb, year, egc_cv):
        # 参考计算公式
        # =IF(C7>3,IF((T58-C6*4+1)>0,VLOOKUP(B58-C6*4,B58:AC158,C41,TRUE)*(1+C8)^4,0),0)
        if not repeat_saving_plan > 0:
            return 0
        else:
            # IF((T58-C6*4+1)>0,VLOOKUP(B58-C6*4,B58:AC158,C41,TRUE)*(1+C8)^4,0)
            if not year - saving_payment_term * 4 + 1 > 0:
                return 0
            else:
                # VLOOKUP(B58-C6*4,B58:AC158,C41,TRUE)*(1+C8)^4
                _val = egc_cv[cal_anb - saving_payment_term * 4]
                return _val * (1 + repeat_saving_growth_rate) ** 4

    # 计算 A. EGC CV 5
    # return: A. EGC CV 5 AH60: A. EGC CV 5
    # Params:
    def get_A_EGC_CV_5(self, saving_payment_term, repeat_saving_plan, repeat_saving_growth_rate, cal_anb, year, egc_cv):
        # 参考计算公式
        # =IF(C7>4,IF((T58-C6*5+1)>0,VLOOKUP(B58-C6*5,B58:AC158,C41,TRUE)*(1+C8)^5,0),0)
        if not repeat_saving_plan > 0:
            return 0
        else:
            # IF((T58-C6*5+1)>0,VLOOKUP(B58-C6*5,B58:AC158,C41,TRUE)*(1+C8)^5,0)
            if not year - saving_payment_term * 5 + 1 > 0:
                return 0
            else:
                # VLOOKUP(B58-C6*5,B58:AC158,C41,TRUE)*(1+C8)^5
                _val = egc_cv[cal_anb - saving_payment_term * 5]
                return _val * (1 + repeat_saving_growth_rate) ** 5

    # 计算 Total A. EGC CV
    # return: Total A. EGC CV AI60: Total A. EGC CV
    # Params:
    def get_Total_A_EGC_CV(self, nums):
        # 参考计算公式
        # =SUM(AC58:AH58)
        return sum(nums)

    # 计算 re_adjusted CV
    # return: re_adjusted CV AJ60: re_adjusted CV
    # Params:
    def get_re_adjusted_CV(self, sa_too_low, last_re_adjusted_cv, total_a_egc_cv):
        # 参考计算公式
        # =IF(S58,AJ57*(1+Notes!B3),AI58)
        if sa_too_low:
            return last_re_adjusted_cv * (1 + EgcObj.RETURN_AFTER_SURRENDER)
        else:
            return total_a_egc_cv

    # 计算 Balance before withdraw
    # return: Balance before withdraw AK60: Balance before withdraw
    # Params:
    def get_Balance_before_withdraw(self, re_adjusted_cv, remain):
        # 参考计算公式
        # =AJ58*Y57
        return re_adjusted_cv * remain

    # 计算 Retired
    # return: Retired AL60: Retired
    # Params:
    def get_Retired(self, anb_year, withdrawal_starting_anb):
        # 参考计算公式
        # =(B58>=F5)
        return anb_year >= withdrawal_starting_anb

    # 计算 Income
    # return: Income AM60: Income
    # Params:
    def get_Income(self, surrender_anb, withdrawal_starting_anb, withdrawal_years, anb_year, retired):
        # 参考计算公式
        # =IF(AL58,IF(F8>0,AND(B58<F3,(B58-F5)<F8),B58<F3),FALSE)
        if not retired:
            return False
        else:
            # IF(F8>0,AND(B58<F3,(B58-F5)<F8),B58<F3)
            if withdrawal_years > 0:
                # AND(B58<F3,(B58-F5)<F8)
                return (anb_year < surrender_anb) and (anb_year - withdrawal_starting_anb) < withdrawal_years
            else:
                return anb_year < surrender_anb
        return 0

    # 计算 Inflation factor
    # return: Inflation factor AN60: Inflation factor
    # Params:
    def get_Inflation_factor(self, withdrawal_starting_anb, Inflation, anb_year, retired):
        # 参考计算公式
        # =IF(AL58,(1+F7)^(B58-F5),1)
        if not retired:
            return 1
        else:
            (1 + Inflation) ** (anb_year - withdrawal_starting_anb)
        return 0

    # 计算 Inflated income
    # return: Inflated income AO60: Inflated income
    # Params:
    # Income? ：AM57
    # Inflation factor ：AN57
    # Display rate ：C33
    # 首年提款 : F6
    def get_Inflated_income(self, display_rate, fst_Year_Withdrawal, inflation_factor, income):
        # 参考计算公式
        # =IF(AM57,F6/C33*AN57,0)
        if not income:
            return 0
        else:
            fst_Year_Withdrawal / display_rate * inflation_factor
        return 0

    # 计算 Withdrawal
    # return: Withdrawal AP60: Withdrawal
    # Params:
    # Inflated income :AO57
    # 額外提取 :D57
    # Balance before withdraw :AK57
    # Display rate :C33
    def get_Withdrawal(self, display_rate, extra_withdrawal, inflated_income, balance_before_withdraw):
        # 参考计算公式
        # =MIN(AO57+D57/C33,AK57)
        numbers = []
        numbers.append(balance_before_withdraw)
        numbers.append(inflated_income + extra_withdrawal / display_rate)
        return min(numbers)

    # 计算 Cashflow
    # return: Cashflow AQ60: Cashflow
    # Params:
    def get_Cashflow(self, anb_year, surrender_anb, cal_anb, annual_contribution, total_withdrawal, balance):
        # 参考计算公式
        # =IF(F3>C3,IF(B58<F3,E58-C58,E58+G58),0)
        if not surrender_anb > anb_year:
            return 0
        else:
            if cal_anb < surrender_anb:
                return total_withdrawal - annual_contribution
            else:
                return total_withdrawal + balance
        return 0

    # **
    # 现金流 Cashflow
    # Params:
    # 退保翌年歲 F3 : Surrender ANB
    # 翌年歲 C3 : anb
    # 翌年歲_year B61 : anb_year
    # 每年供款 C61 : Annual Contribution
    # 總提取 E61 : Total Withdrawal
    # 餘額 G61 : Balance
    def get_cashflow(self, anb, anb_year, surrender_anb, total_withdrawal, annual_contribution, balance, ):
        # 参考计算公式
        # # =IF(F3>C3,IF(B57<F3,E57-C57,E57+G57),0)
        if not surrender_anb > anb:
            return 0
        else:
            return total_withdrawal - annual_contribution if anb_year < surrender_anb else total_withdrawal + balance

    # 粘贴生成的方法内容 --------------------------------------------------------------------------------------------   END

    # **
    # 说明:
    @staticmethod
    def egc_master(str_json_key, args: list[str], years, param_Extra_Withdrawal: list[float],
                   param_withdrawal_anb: list[int], ):
        from datetime import datetime
        TEST_SELECT_ANB = float(args[0])  # 翌年歲
        TEST_ANNUAL_SAVING = float(args[1])  # 每年儲蓄金額
        TEST_SAVING_PAYMENT_TERM = float(args[2])  # 儲蓄供款期
        TEST_REPEAT_SAVING_PLAN = float(args[3])  # 重複儲蓄計劃
        TEST_REPEAT_SAVING_GROWTH_RATE = float(args[4])  # 重複儲蓄增長率
        TEST_SAVING_SUM_ASSURED = float(args[5])  # 儲蓄名義金額
        TEST_CURRENCY = args[6]  # 貨幣
        TEST_SURRENDER_ANB = float(args[7])  # 退保翌年歲
        TEST_WITHDRAWAL_STARTING_ANB = float(args[8])  # 提款開始翌年歲
        TEST_FST_YEAR_WITHDRAWAL = float(args[9])  # 首年提款
        TEST_INFLATION = float(args[10])  # 通脹率
        TEST_WITHDRAWAL_YEARS = float(args[11])  # 提取年期
        TEST_PROMOTION = args[12]  # 推廣優惠
        TEST_LUMP_SUM_PREPAID = args[13]  # 一筆過預繳
        TEST_EXTRA_WITHDRAWAL = float(args[14])  # 額外提取
        calStd = EgcBaseTabCalStd()
        try:
            # 获取外部参数
            cal_Display_rate = base_table_top.get_Display_rate(currency=TEST_CURRENCY)
            cal_SA = base_table_top.get_SA(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                           annual_saving=TEST_ANNUAL_SAVING,
                                           display_rate=cal_Display_rate)
            cal_SA_minimum = base_table_top.get_SA_minimum(currency=TEST_CURRENCY)
            cal_EGC_discount = base_table_top.get_EGC_discount()
            cal_discount_adjusted_premium = base_table_top.get_discount_adjusted_premium(sa=cal_SA,
                                                                                         saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                                                         egc_discount=cal_EGC_discount, )
            cal_discount_adjusted_SA = base_table_top.get_discount_adjusted_SA(sa=cal_SA,
                                                                               annual_saving=TEST_ANNUAL_SAVING,
                                                                               discount_adjusted_premium=cal_discount_adjusted_premium,
                                                                               display_rate=cal_Display_rate)
            cal_Saving_Sum_Assured = base_table_top.get_Saving_Sum_Assured(
                cal_discount_adjusted_SA=cal_discount_adjusted_SA)
            cal_lumsum = base_table_top.get_lumsum(lump_sum_prepaid=TEST_LUMP_SUM_PREPAID,
                                                   saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                   currency=TEST_CURRENCY)
            cal_EGC_annual_premium = base_table_top.get_EGC_annual_premium(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                                           saving_sum_assured=cal_Saving_Sum_Assured,
                                                                           egc_discount=cal_EGC_discount)
            cal_lumpsum_discount = base_table_top.get_lumpsum_discount(lumsum=cal_lumsum,
                                                                       currency=TEST_CURRENCY,
                                                                       egc_annual_premium=cal_EGC_annual_premium)
            cal_EGC_SA_factor = base_table_top.get_EGC_SA_factor(saving_sum_assured=cal_Saving_Sum_Assured,
                                                                 saving_payment_term=TEST_SAVING_PAYMENT_TERM)

            # ----------------------------------------------------------------------------------------------------------
            rows = []
            last_Remain = 1  # 这里是循环数据处理的前置数据
            now = datetime.now()
            current_time = now.strftime("%H:%M:%S")
            print("当前时间 =", current_time)
            save_db_flg = True
            # ----------------------------------------------------------------------------------------------------------
            _remains = {}
            _EGC_premium_2s = {}
            _cumulative_premiums = {}
            _re_adjusted_CVs = {}
            _Cumulative_Withdrawal = {}
            _Balance = {}
            _Total_A_EGC_CV = {}
            for test_year in years:
                one_Total_Withdrawal = 0  # 總提取
                one_Cumulative_Withdrawal = 0  # 累積提取
                cal_Extra_Withdrawal = 0  # 額外提取
                single_row_data = {}  # 页面数据行
                anb_year = (TEST_SELECT_ANB) + test_year  # 计算投资起始年龄
                # 获取复制的业务代码内容------------------------------------------------------------------------------ START
                if test_year == 3:
                    print("123")
                # 判定计算当年是否存在提现，提现参数默认为0，否则需要计算提现后的参数
                try:
                    if param_withdrawal_anb.index(anb_year) >= 0:
                        get_data_index = param_withdrawal_anb.index(anb_year)
                        cal_Extra_Withdrawal = param_Extra_Withdrawal[get_data_index]
                        withdrawal_cal = True
                        save_db_flg = False  # 没有提现数据则可作为标准数据写入计算参数组合标准数据库
                except ValueError as e1:
                    withdrawal_cal = False
                print("run test Year with : %d" % test_year)
                cal_Year = calStd.get_Year(anb_year=anb_year, cal_anb=TEST_SELECT_ANB)
                # Col 11 计算 SA 0
                one_SA_0 = calStd.get_SA_0(cal_Saving_Sum_Assured)
                single_row_data["one_SA_0_res"] = round(one_SA_0, 2)
                # Col 12 计算 SA 1
                one_SA_1 = calStd.get_SA_1(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                           repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                           repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                           saving_sum_assured=cal_Saving_Sum_Assured,
                                           year=cal_Year)
                single_row_data["one_SA_1_res"] = round(one_SA_1, 2)
                # Col 13 计算 SA 2
                one_SA_2 = calStd.get_SA_2(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                           repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                           repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                           saving_sum_assured=cal_Saving_Sum_Assured,
                                           year=cal_Year)
                single_row_data["one_SA_2_res"] = round(one_SA_2, 2)
                # Col 14 计算 SA 3
                one_SA_3 = calStd.get_SA_3(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                           repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                           repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                           saving_sum_assured=cal_Saving_Sum_Assured,
                                           year=cal_Year)
                single_row_data["one_SA_3_res"] = round(one_SA_3, 2)
                # Col 15 计算 SA 4
                one_SA_4 = calStd.get_SA_4(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                           repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                           repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                           saving_sum_assured=cal_Saving_Sum_Assured,
                                           year=cal_Year)
                single_row_data["one_SA_4_res"] = round(one_SA_4, 2)
                # Col 16 计算 SA 5
                one_SA_5 = calStd.get_SA_5(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                           repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                           repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                           saving_sum_assured=cal_Saving_Sum_Assured,
                                           year=cal_Year)
                single_row_data["one_SA_5_res"] = round(one_SA_5, 2)
                # Col 17 计算 Total min SA
                one_Total_min_SA = calStd.get_Total_min_SA(year=cal_Year,
                                                           saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                           repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                                           sa_minimum=cal_SA_minimum)
                single_row_data["one_Total_min_SA_res"] = round(one_Total_min_SA, 2)
                _array_SA = [one_SA_0, one_SA_1, one_SA_2, one_SA_3, one_SA_4, one_SA_5]
                # Col 18 计算 Running SA
                if test_year == 0:
                    cal_last_remain = float(100 / 100)
                else:
                    cal_last_remain = _remains[test_year - 1]  # 从多年份数据中直接获取数据
                one_Running_SA = calStd.get_Running_SA(_array_SA, last_remain=cal_last_remain)
                single_row_data["one_Running_SA_res"] = round(one_Running_SA, 2)
                # Col 19 计算 SA too low
                one_SA_too_low = calStd.get_SA_too_low(running_sa=one_Running_SA, total_min_sa=one_Total_min_SA)
                single_row_data["one_SA_too_low_res"] = one_SA_too_low
                # Col 20 计算 Year
                one_Year = cal_Year
                single_row_data["one_Year_res"] = round(one_Year, 2)
                # Col 21 计算 Rebate
                cal_egc_premium_2 = calStd.get_EGC_premium_2(cal_anb=anb_year,
                                                             anb_year=TEST_SELECT_ANB,
                                                             saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                             repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                                             repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                                             egc_annual_premium=cal_EGC_annual_premium)
                if test_year == 0:
                    one_Rebate = 0
                else:
                    one_Rebate = calStd.get_Rebate(year=test_year, saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                   display_rate=cal_Display_rate,
                                                   egc_annual_premium=cal_EGC_annual_premium,
                                                   promotion=TEST_PROMOTION,
                                                   egc_premium_2=cal_egc_premium_2)
                single_row_data["one_Rebate_res"] = round(one_Rebate, 2)
                # Col 22 计算 EGC premium
                one_EGC_premium_1 = calStd.get_EGC_premium_1(year=test_year,
                                                             anb_year=anb_year,
                                                             saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                             lumsum=cal_lumsum,
                                                             lumpsum_discount=cal_lumpsum_discount,
                                                             display_rate=cal_Display_rate,
                                                             rebate=one_Rebate,
                                                             egc_premium_2=cal_egc_premium_2)
                single_row_data["one_EGC_premium_1_res"] = round(one_EGC_premium_1, 2)
                # Col 23 计算 Cumulative premium
                if test_year == 0:
                    last_cumulative_premium = 0
                else:
                    last_cumulative_premium = _cumulative_premiums[test_year - 1]
                one_Cumulative_premium = calStd.get_Cumulative_premium(last_cumulative_premium=last_cumulative_premium,
                                                                       egc_premium_1=one_EGC_premium_1)
                single_row_data["one_Cumulative_premium_res"] = round(one_Cumulative_premium, 2)
                _cumulative_premiums[test_year] = one_Cumulative_premium
                # TODO 这里插队提前计算SA系数数据 START  ———————————————————————————————————————————————————————————————————
                one_EGC_CV = calStd.get_EGC_CV(year=test_year, ANB=TEST_SELECT_ANB,
                                               anb_year=anb_year, saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                               egc_sa_factor=cal_EGC_SA_factor)
                single_row_data["one_EGC_CV_res"] = round(one_EGC_CV, 2)
                one_Adjusted_EGC_CV = calStd.get_Adjusted_EGC_CV(one_EGC_CV)
                # Col 30 计算 A. EGC CV 1
                one_A_EGC_CV_1 = calStd.get_A_EGC_CV_1(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                       repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                                       repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                                       cal_anb=anb_year, year=test_year, egc_cv=one_Adjusted_EGC_CV)
                single_row_data["one_A_EGC_CV_1_res"] = round(one_A_EGC_CV_1, 2)
                # Col 31 计算 A. EGC CV 2
                one_A_EGC_CV_2 = calStd.get_A_EGC_CV_2(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                       repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                                       repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                                       cal_anb=anb_year, year=test_year, egc_cv=one_EGC_CV)
                single_row_data["one_A_EGC_CV_2_res"] = round(one_A_EGC_CV_2, 2)
                # Col 32 计算 A. EGC CV 3
                one_A_EGC_CV_3 = calStd.get_A_EGC_CV_3(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                       repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                                       repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                                       cal_anb=anb_year, year=test_year, egc_cv=one_EGC_CV)
                single_row_data["one_A_EGC_CV_3_res"] = round(one_A_EGC_CV_3, 2)
                # Col 33 计算 A. EGC CV 4
                one_A_EGC_CV_4 = calStd.get_A_EGC_CV_4(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                       repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                                       repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                                       cal_anb=anb_year, year=test_year, egc_cv=one_EGC_CV)
                single_row_data["one_A_EGC_CV_4_res"] = round(one_A_EGC_CV_4, 2)
                # Col 34 计算 A. EGC CV 5
                one_A_EGC_CV_5 = calStd.get_A_EGC_CV_5(saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                       repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                                       repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                                       cal_anb=anb_year, year=test_year, egc_cv=one_EGC_CV)
                single_row_data["one_A_EGC_CV_5_res"] = round(one_A_EGC_CV_5, 2)
                # Col 35 计算 Total A. EGC CV
                _array_A_EGC_CV = [one_Adjusted_EGC_CV, one_A_EGC_CV_1, one_A_EGC_CV_2, one_A_EGC_CV_3, one_A_EGC_CV_4,
                                   one_A_EGC_CV_5]
                one_Total_A_EGC_CV = calStd.get_Total_A_EGC_CV(_array_A_EGC_CV)
                single_row_data["one_Total_A_EGC_CV_res"] = round(one_Total_A_EGC_CV, 2)
                _Total_A_EGC_CV[test_year] = one_Total_A_EGC_CV
                # Col 36 计算 re-adjusted CV
                if test_year == 0:
                    last_re_adjusted_cv = 0
                else:
                    last_re_adjusted_cv = _re_adjusted_CVs[test_year - 1]

                one_re_adjusted_CV = calStd.get_re_adjusted_CV(sa_too_low=one_SA_too_low,
                                                               last_re_adjusted_cv=last_re_adjusted_cv,
                                                               total_a_egc_cv=one_Total_A_EGC_CV)
                single_row_data["one_re_adjusted_CV_res"] = round(one_re_adjusted_CV, 2)
                _re_adjusted_CVs[test_year] = one_re_adjusted_CV
                # TODO 这里插队提前计算SA系数数据 END  ———————————————————————————————————————————————————————————————————
                # TODO Col 24 计算 Annual withdrawal 重新顺序计算
                one_Annual_withdrawal = calStd.get_Annual_withdrawal(re_adjusted_cv=one_re_adjusted_CV,
                                                                     total_withdrawal=one_Total_Withdrawal,
                                                                     display_rate=cal_Display_rate, )
                single_row_data["one_Annual_withdrawal_res"] = round(one_Annual_withdrawal, 2)
                # Col 25 计算 Remain
                if test_year > 0:
                    last_Remain = _remains[test_year - 1]
                one_Remain = calStd.get_Remain_(last_remain=last_Remain, annual_withdrawal=one_Annual_withdrawal)
                single_row_data["one_Remain_res"] = round(one_Remain, 2)
                _remains[test_year] = one_Remain
                # Col 26 计算 Balance
                one_Balance = calStd.get_Balance(remain=one_Remain, re_adjusted_cv=one_re_adjusted_CV)
                single_row_data["one_Balance_res"] = round(one_Balance, 2)
                # Col 27 计算 EGC premium
                one_EGC_premium_2 = calStd.get_EGC_premium_2(cal_anb=anb_year,
                                                             anb_year=TEST_SELECT_ANB,
                                                             saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                                             repeat_saving_plan=TEST_REPEAT_SAVING_PLAN,
                                                             repeat_saving_growth_rate=TEST_REPEAT_SAVING_GROWTH_RATE,
                                                             egc_annual_premium=cal_EGC_annual_premium)
                _EGC_premium_2s[test_year] = one_EGC_premium_2
                single_row_data["one_EGC_premium_res_2"] = round(one_EGC_premium_2, 2)
                # Col 28 计算 EGC CV
                one_EGC_CV = calStd.get_EGC_CV(year=test_year, ANB=TEST_SELECT_ANB,
                                               anb_year=anb_year, saving_payment_term=TEST_SAVING_PAYMENT_TERM,
                                               egc_sa_factor=cal_EGC_SA_factor)
                single_row_data["one_EGC_CV_res"] = round(one_EGC_CV, 2)
                # Col 29 计算 Adjusted EGC CV
                one_Adjusted_EGC_CV = calStd.get_Adjusted_EGC_CV(one_EGC_CV)
                single_row_data["one_Adjusted_EGC_CV_res"] = round(one_Adjusted_EGC_CV, 2)
                # ——————————————————————————————————————————————————————————————————————————————————————————————————
                # Col 37 计算 Balance before withdraw
                one_Balance_before_withdraw = calStd.get_Balance_before_withdraw(re_adjusted_cv=one_re_adjusted_CV,
                                                                                 remain=cal_last_remain)
                single_row_data["one_Balance_before_withdraw_res"] = round(one_Balance_before_withdraw, 2)
                # Col 38 计算 Retired
                one_Retired = calStd.get_Retired(anb_year=anb_year,
                                                 withdrawal_starting_anb=TEST_WITHDRAWAL_STARTING_ANB)
                single_row_data["one_Retired_res"] = one_Retired
                # Col 39 计算 Income
                one_Income = calStd.get_Income(surrender_anb=TEST_SURRENDER_ANB, anb_year=anb_year, retired=one_Retired,
                                               withdrawal_starting_anb=TEST_WITHDRAWAL_STARTING_ANB,
                                               withdrawal_years=TEST_WITHDRAWAL_YEARS, )
                single_row_data["one_Income_res"] = one_Income
                # Col 40 计算 Inflation factor
                one_Inflation_factor = calStd.get_Inflation_factor(withdrawal_starting_anb=TEST_WITHDRAWAL_STARTING_ANB,
                                                                   Inflation=TEST_INFLATION,
                                                                   anb_year=anb_year,
                                                                   retired=one_Retired)
                single_row_data["one_Inflation_factor_res"] = round(one_Inflation_factor, 2)
                # Col 41 计算 Inflated income
                one_Inflated_income = calStd.get_Inflated_income(display_rate=cal_Display_rate,
                                                                 fst_Year_Withdrawal=TEST_FST_YEAR_WITHDRAWAL,
                                                                 inflation_factor=one_Inflation_factor,
                                                                 income=one_Income)
                single_row_data["one_Inflated_income_res"] = round(one_Inflated_income, 2)
                # Col 42 计算 Withdrawal
                one_Withdrawal = calStd.get_Withdrawal(display_rate=cal_Display_rate,
                                                       extra_withdrawal=TEST_EXTRA_WITHDRAWAL,
                                                       inflated_income=one_Inflated_income,
                                                       balance_before_withdraw=one_Balance_before_withdraw)
                single_row_data["one_Withdrawal_res"] = round(one_Withdrawal, 2)
                # ------------------------------------------------------------------------------------------------------
                # chg_tab Area
                # Col1 年期
                single_row_data['chgtab_Year_res'] = test_year
                # Col2 翌年歲
                single_row_data['chgtab_ANB_res'] = anb_year
                # Col3 每年供款
                one_Annual_Contribution = one_EGC_premium_1
                single_row_data['chgtab_Annual_Contribution_res'] = one_Annual_Contribution
                # Col4 額外提取
                single_row_data['chgtab_Extra_Withdrawal_res'] = cal_Extra_Withdrawal
                # Col5 總提取
                # =IF(B57<202,AP57,0)*C33
                one_Total_Withdrawal = one_Withdrawal * cal_Display_rate if anb_year < 202 else 0
                single_row_data['chgtab_Total_Withdrawal_res'] = one_Total_Withdrawal
                # Col6 累積提取
                # =IF(AND(B57<202,B57<=F3),F56+E57,0)
                if len(rows) > 0:
                    last_Cumulative_Withdrawal = _Cumulative_Withdrawal[test_year - 1]
                    one_Cumulative_Withdrawal = last_Cumulative_Withdrawal + one_Total_Withdrawal if (
                            anb_year < 202 and anb_year < TEST_SURRENDER_ANB) else 0
                single_row_data['chgtab_Cumulative_Withdrawal_res'] = one_Cumulative_Withdrawal
                _Cumulative_Withdrawal[test_year] = one_Cumulative_Withdrawal
                # Col7 餘額
                # =IF(AND(B57<202,B57<=F3),Z57,0)*C33
                one_chgtab_Balance = one_Balance if (anb_year < 202 and anb_year <= TEST_SURRENDER_ANB) else 0
                one_chgtab_Balance = one_chgtab_Balance * cal_Display_rate
                single_row_data['chgtab_Balance_res'] = round(one_chgtab_Balance, 3)
                _Balance[test_year] = one_chgtab_Balance
                # Col8 年對年回報
                # =IF(AND(B57<202,B57<=F3,G56>0,OR(AI56*C33>C13,A57>100)),AK57/(G56/C33)-1,0)
                # AND(B57<202,B57<=F3,G56>0,OR(AI56*C33>C13,A57>100))
                if test_year > 0:
                    if anb_year < 202 and anb_year <= TEST_SURRENDER_ANB and _Balance[test_year - 1] > 0 and (
                            one_Balance_before_withdraw / _Total_A_EGC_CV[test_year - 1]):
                        # AK57/(G56/C33)-1
                        one_Year_Over_Year_Growth_Rate = one_Balance_before_withdraw
                else:
                    one_Year_Over_Year_Growth_Rate = 0
                single_row_data['chgtab_Year_Over_Year_Growth_Rate_res'] = round(one_Year_Over_Year_Growth_Rate, 3)
                # chg_tab Area

                # ------------------------------------------------------------------------------------------------------
                # # 验证是否需要提现计算
                if withdrawal_cal:
                    print("计算提现后数据")
                    # 1 Withdrawal
                    chg_Withdrawal = calStd.get_Withdrawal(display_rate=cal_Display_rate,
                                                           extra_withdrawal=TEST_EXTRA_WITHDRAWAL,
                                                           inflated_income=one_Inflated_income,
                                                           balance_before_withdraw=one_Balance_before_withdraw)
                    single_row_data["one_Withdrawal_res"] = round(chg_Withdrawal, 2)
                    # 2 總提取 Total Withdrawal
                    chg_Total_Withdrawal = chg_Withdrawal * cal_Display_rate if anb_year < 202 else 0
                    single_row_data['chgtab_Total_Withdrawal_res'] = chg_Total_Withdrawal
                    # 3 累積提取 Cumulative Withdrawal
                    if len(rows) > 0:
                        last_Cumulative_Withdrawal = _Cumulative_Withdrawal[test_year - 1]
                        chg_Cumulative_Withdrawal = last_Cumulative_Withdrawal + chg_Total_Withdrawal if (
                                anb_year < 202 and anb_year < TEST_SURRENDER_ANB) else 0
                    single_row_data['chgtab_Cumulative_Withdrawal_res'] = chg_Cumulative_Withdrawal
                    _Cumulative_Withdrawal[test_year] = chg_Cumulative_Withdrawal
                    # 4 Annual withdrawal %
                    chg_Annual_withdrawal = calStd.get_Annual_withdrawal(re_adjusted_cv=one_re_adjusted_CV,
                                                                         total_withdrawal=chg_Total_Withdrawal,
                                                                         display_rate=cal_Display_rate, )
                    single_row_data["one_Annual_withdrawal_res"] = round(chg_Annual_withdrawal, 2)
                    # 5 Remain
                    chg_Remain = calStd.get_Remain_(last_remain=last_Remain, annual_withdrawal=chg_Annual_withdrawal)
                    single_row_data["one_Remain_res"] = round(chg_Remain, 2)
                    # 6 Balance
                    chg_Balance = calStd.get_Balance(remain=chg_Remain, re_adjusted_cv=one_re_adjusted_CV)
                    single_row_data["one_Balance_res"] = round(chg_Balance, 2)

                # ------------------------------------------------------------------------------------------------------
                # Col 41 Cashflow 这里需要最后去计算，数据来源前置总表格
                one_Cashflow = calStd.get_cashflow(surrender_anb=TEST_SURRENDER_ANB, anb=TEST_SELECT_ANB,
                                                   anb_year=anb_year,
                                                   total_withdrawal=one_Total_Withdrawal,
                                                   annual_contribution=one_Annual_Contribution,
                                                   balance=one_Balance)
                single_row_data['one_Cashflow_res'] = round(one_Cashflow, 3)
                rows.append(single_row_data)
            # 获取复制的业务代码内容------------------------------------------------------------------------------   END
            # ----------------------------------------------------------------------------------------------------------
            now_2 = datetime.now()
            current_time_2 = now_2.strftime("%H:%M:%S")
            print("当前时间 =", current_time_2)
            str_json_data = json.dumps(rows, ensure_ascii=False)
            if save_db_flg:
                # dict_db.insert_cal_json_data([str_json_key, str_json_data, str(datetime.now().strftime("%f"))])
                print("Saving Data")
            # 保持一致同步，写入到RDS中
            RedisConfig.push_val_intimes(rds_cfg, key=f'freeL_hash_key:{str_json_key}', val=str_json_data,
                                         json_flg=True, inTimes=60 * 60)
            return {"code": 200, "data": str_json_data}
        except Exception as e:
            print(e)

    def cal_testing_data(self, args: list[str], total_a_egc_cv):
        try_obj = {"min": 0, "max": 0, "try value": 0, "enough": 0, "next value": 0, }
        TEST_SELECT_ANB = float(args[0])  # 翌年歲
        TEST_ANNUAL_SAVING = float(args[1])  # 每年儲蓄金額
        TEST_SAVING_PAYMENT_TERM = float(args[2])  # 儲蓄供款期
        TEST_REPEAT_SAVING_PLAN = float(args[3])  # 重複儲蓄計劃
        TEST_REPEAT_SAVING_GROWTH_RATE = float(args[4])  # 重複儲蓄增長率
        TEST_SAVING_SUM_ASSURED = float(args[5])  # 儲蓄名義金額
        TEST_CURRENCY = float(args[6])  # 貨幣
        TEST_SURRENDER_ANB = float(args[7])  # 退保翌年歲
        TEST_WITHDRAWAL_STARTING_ANB = float(args[8])  # 提款開始翌年歲
        TEST_FST_YEAR_WITHDRAWAL = float(args[9])  # 首年提款
        TEST_INFLATION = float(args[10])  # 通脹率
        TEST_WITHDRAWAL_YEARS = float(args[11])  # 提取年期
        TEST_PROMOTION = float(args[12])  # 推廣優惠
        TEST_LUMP_SUM_PREPAID = float(args[13])  # 一筆過預繳
        TEST_EXTRA_WITHDRAWAL = float(args[14])  # 額外提取
        cal_max_withdrawal_age = base_table_top.get_max_withdrawal_age(surrender_anb=TEST_SURRENDER_ANB,
                                                                       withdrawal_starting_anb=TEST_WITHDRAWAL_STARTING_ANB,
                                                                       withdrawal_years=TEST_WITHDRAWAL_YEARS)
        tryObjs = {}
        for _index in range(10):
            one_t_obj = try_obj
            if _index == 0:
                cal_surrender_value = base_table_top.get_surrender_value(rows=total_a_egc_cv,
                                                                         max_withdrawal_age=cal_max_withdrawal_age, )
                cal_seed_value = base_table_top.get_seed_value(surrender_value=cal_surrender_value,
                                                               surrender_anb=TEST_SURRENDER_ANB,
                                                               withdrawal_starting_anb=TEST_WITHDRAWAL_STARTING_ANB,
                                                               withdrawal_years=TEST_WITHDRAWAL_YEARS)
                one_t_obj["min"] = 0
                one_t_obj["max"] = cal_seed_value * 2
                one_t_obj["try_value"] = cal_seed_value * 2
                one_t_obj["enough"] = False
                one_t_obj["next_value"] = cal_seed_value
                tryObjs[_index] = one_t_obj
